package com.yuanye.gwcit;

import android.app.Application;
import android.os.Environment;

import androidx.annotation.NonNull;
import androidx.camera.camera2.Camera2Config;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.CameraXConfig;

import com.elvishew.xlog.LogConfiguration;
import com.elvishew.xlog.LogLevel;
import com.elvishew.xlog.XLog;
import com.elvishew.xlog.flattener.Flattener2;
import com.elvishew.xlog.printer.AndroidPrinter;
import com.elvishew.xlog.printer.Printer;
import com.elvishew.xlog.printer.file.FilePrinter;
import com.elvishew.xlog.printer.file.naming.FileNameGenerator;
import com.yuanye.gwcit.utils.GT;
import com.yuanye.yeecho.utils.YY;

public class BaseApplication extends Application implements CameraXConfig.Provider{
    @Override
    public void onCreate() {
        super.onCreate();
        GW.sp = GT.getSP(this);
        initXLog();
    }

    protected void initXLog(){
        LogConfiguration config = new LogConfiguration.Builder()
                .logLevel(LogLevel.ALL)
                .build();
        Printer logcat = new AndroidPrinter(true);
        Printer filePrinter = new FilePrinter.Builder(Environment.getExternalStorageDirectory().getAbsolutePath())
                .fileNameGenerator(new MyFileNameGenerator())
                .flattener(new Flattener2() {
                    @Override
                    public CharSequence flatten(long timeMillis, int logLevel, String tag, String message) {
                        return YY.simpleDateFormat.format(timeMillis)
                                + ' ' + LogLevel.getShortLevelName(logLevel)
                                + '/' + tag
                                + ": " + message;
                    }
                })
                .build();
        XLog.init(config, logcat, filePrinter);
    }

    @NonNull
    @Override
    public CameraXConfig getCameraXConfig() {
        CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_EXTERNAL).build();

        return CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
                .setAvailableCamerasLimiter(cameraSelector)
                .build();
    }

    class MyFileNameGenerator implements FileNameGenerator {

        @Override
        public boolean isFileNameChangeable() {
            return true;
        }

        @Override
        public String generateFileName(int logLevel, long timestamp) {
            String fileName = GW.LOGFILE_NAME;
//            switch (funType){
//                case PCBA:
//                    fileName = "PCBA_TEST.log";
//                    break;
//                case AGING:
//                    fileName = "AGING_TEST.log";
//                    break;
//                case PRODUCT:
//                    fileName = "PRODUCT_TEST.log";
//                    break;
//            }
            return fileName;
        }
    }
}
